﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Collections;

namespace GMS.BIZ
{
    public class Jobs
    {
        private string studentID;

        /// <summary>
        /// 初始化Job
        /// </summary>
        /// <param name="studentID">学生学号</param>
        public Jobs(string studentID)
        {
            this.studentID = studentID;
        }

        /// <summary>
        /// 数该学生有多少条就业意向的记录
        /// </summary>
        public int Count()
        {
            GMS.ORM.DAL.GJob optGJob = new GMS.ORM.DAL.GJob();
            DataSet ds = optGJob.GetList("std_id='" + this.studentID + "' ORDER BY job_time DESC");
            try
            {
                return ds.Tables[0].Rows.Count;
            }
            catch
            {
                return 0;
            }
        }

        /// <summary>
        /// 添加一条学生就业意向
        /// </summary>
        /// <param name="inputJob">就业的对象</param>
        public string Add(Job inputJob)
        {
            GMS.ORM.Model.GJob myGJob = new GMS.ORM.Model.GJob();
            myGJob.std_id = this.studentID;
            myGJob.job_level = inputJob.job_level;
            myGJob.job_time = inputJob.job_time;

            GMS.ORM.DAL.GJob optGJob = new GMS.ORM.DAL.GJob();
            bool ok = optGJob.Add(myGJob);
            if (!ok) return ErrorMessage.DBOptError;
            else return ErrorMessage.OK;
        }

        /// <summary>
        /// 删除一条学生就业意向
        /// </summary>
        /// <param name="id">删除的id号</param>
        public string Delete(decimal id)
        {
            GMS.ORM.DAL.GJob optGJob = new GMS.ORM.DAL.GJob();
            bool ok = optGJob.Delete(id);
            if (!ok) return ErrorMessage.DBOptError;
            else return ErrorMessage.OK;
        }

        /// <summary>
        /// 修改一条学生就业意向
        /// </summary>
        /// <param name="inputJob">就业意向的对象</param>
        public string Update(Job inputJob)
        {
            GMS.ORM.Model.GJob myGJob = new GMS.ORM.Model.GJob();
            myGJob.id = inputJob.id;
            myGJob.std_id = this.studentID;
            myGJob.job_level = inputJob.job_level;
            myGJob.job_time = inputJob.job_time;
            
            GMS.ORM.DAL.GJob optGJob = new GMS.ORM.DAL.GJob();
            bool ok = optGJob.Update(myGJob);
            if (!ok) return ErrorMessage.DBOptError;
            else return ErrorMessage.OK;
        }

        /// <summary>
        /// 获得一条学生的就业意向
        /// </summary>
        /// <param name="id">id</param>
        public Job GetOne(decimal id)
        {
            Job outputJob = new Job();
            GMS.ORM.Model.GJob myGJob = new GMS.ORM.Model.GJob();
            GMS.ORM.DAL.GJob optGJob = new GMS.ORM.DAL.GJob();
            try
            {
                myGJob = optGJob.GetModel(id);
                outputJob.id = myGJob.id;
                outputJob.std_id = myGJob.std_id;
                outputJob.job_level = myGJob.job_level;
                outputJob.job_time = myGJob.job_time;
                return outputJob;
            }
            catch
            {
                return null;
            }
        }

        /// <summary>
        /// 获得一个学生的就业意向的数组
        /// </summary>
        public ArrayList GetList()
        {
            ArrayList returnList = new ArrayList();

            GMS.ORM.DAL.GJob optGJob = new GMS.ORM.DAL.GJob();
            DataSet ds = optGJob.GetList("std_id='" + this.studentID + "' ORDER BY job_time DESC");
            if (ds.Tables[0].Rows.Count == 0)
                return returnList;
            else
            {
                int howMany = ds.Tables[0].Rows.Count;
                for (int i = 0; i < howMany; i++)
                    returnList.Add(decimal.Parse(ds.Tables[0].Rows[i]["id"].ToString()));
                return returnList;
            }
        }

        /// <summary>
        /// 获得一个学生的英语能力的DataSet
        /// </summary>
        public DataSet GetDataSet()
        {
            GMS.ORM.DAL.GJob optGJob = new GMS.ORM.DAL.GJob();
            DataSet ds = optGJob.GetList("std_id='" + this.studentID + "' ORDER BY job_time DESC");
            return ds;
        }

        /// <summary>
        /// 返回学生的就业意向：id,std_id,job_level,job_time
        /// </summary>
        /// <param name="studentID">学生学号，可以为空</param>
        /// <param name="job_level">求职意向，可以为空</param>
        /// <param name="after">在此时间之后</param>
        /// <param name="before">在此时间之前</param>
        static public DataSet GetDataSet(string studentID, string job_level, DateTime? after, DateTime? before)
        {
            string queryString = "1=1";
            if (studentID != string.Empty) queryString = queryString + " AND std_id='" + studentID + "'";
            if (job_level != string.Empty) queryString = queryString + " AND job_level='" + job_level + "'";
            if (after != null) queryString = queryString + " AND job_time>" + after.ToString();
            if (after != null) queryString = queryString + " AND job_time<" + before.ToString();

            GMS.ORM.DAL.GJob optGJob = new GMS.ORM.DAL.GJob();
            return optGJob.GetList(queryString + " ORDER BY job_time");
        }
    }
}
